﻿function Comment(p) {
    var self = this;
    this.parent = p;
    this.body = ko.observable().extend({ required: "Please enter comment" });;
    this.id = ko.observable();
    this.fileId = ko.observable();
    this.name = ko.observable().extend({ required: "Please enter name" });
    this.time = ko.observable();
    this.ip = ko.observable();
    this.url = ko.observable();
    this.isAuthor = ko.observable();
    this.isApproved = ko.observable();
    this.selected = ko.observable();

    this.successMessage = ko.observable();
    this.errors = ko.observableArray();
    this.errorMessage = ko.computed({
        read: function () {
            var html = '';
            if (this.errors().length > 0)
                html += this.errors()[0];
            if (this.errors().length > 1) {
                html += "<ul>"
                for (var idx = 1; idx < this.errors().length; idx++)
                    html += "<li>" + this.errors()[idx] + "</li>";
                html += "</ul>";
            }
            return html;
        },
        write: function (value) {
            this.errors.removeAll();
            this.errors.push(value);
        },
        owner: this
    });


    this.isOdd = ko.observable(true);
    this.isEdit = ko.observable(false);

    this.oldName = ko.observable();
    this.oldBody = ko.observable();
    this.oldUrl = ko.observable();
    this.oldIsAuthor = ko.observable();

    this.load = function (item) {
        this.id(item.ID);
        this.fileId(item.FileID);
        this.name(item.Name);
        this.body(item.Text);
        this.time(item.FormattedTime);
        this.url(item.Url);
        this.ip(item.Ip);
        this.isAuthor(item.IsAuthor);
        this.isApproved(item.IsApproved);
    }

    this.edit = function () {
        this.isEdit(true);
        this.oldName(this.name());
        this.oldBody(this.body());
        this.oldUrl(this.url());
        this.oldIsAuthor(this.isAuthor());
    }

    this.hasError = ko.computed(function () {
        return this.name.hasError() || this.body.hasError();
    }, this);

    this.setError = function () {
        if (this.hasError()) {
            self.parent.errorMessage("Please correct the following errors:");
            if (this.name.hasError())
                self.parent.errors.push(this.name.validationMessage());
            if (this.body.hasError())
                self.parent.errors.push(this.body.validationMessage());
        } else {
            this.clearError();
        }
    }

    this.clearError = function () {
        self.parent.errors.removeAll();
    }

    this.select = function () {
        this.parent.updateSelected();
        return true;
    }

    this.update = function () {
        if (this.hasError()) {
            this.setError();
        } else {
            var url = appPath + 'Comment/Update/'
            var input = {
                olid: this.id(),
                ID: this.id(),
                FileID: this.fileId(),
                Name: this.name(),
                Url: this.url(),
                FormattedTime: this.time(),
                RawComment: this.body(),
                Ip: this.ip(),
                IsApproved: this.isApproved(),
                IsAuthor: this.isAuthor()
            };

            $.post(url, input, function (data) {
                if (data.Result) {
                    // self.parent.redoOdd(); -- not required
                    self.parent.successMessage(data.Message);
                    self.clearError();
                    setInterval(function () { self.parent.successMessage('') }, settings.fadeOutInterval);
                    self.isEdit(false);
                } else {
                    self.parent.errorMessage(data.Message);
                    if (data.Errors) {
                        for (var idx = 0; idx < data.Errors.length; idx++)
                            self.parent.errors.push(data.Errors[idx]);
                    }
                }
            });
        }
    }



    this.close = function () {
        this.clearError();
        if (this.isEdit()) {
            this.isEdit(false);
            this.name(this.oldName());
            this.url(this.oldUrl());
            this.isAuthor(this.oldIsAuthor());
            this.body(this.oldBody());
        }
    }
}

